
 /* global getCookie */
template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};

template.defaults.imports.timestampTransfor = function (timestamp) {
    var d = new Date(timestamp); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + 
    (d.getMonth() + 1) + "-" +
    (d.getDate()) + " " + 
    (d.getHours()) + ":" + 
    (d.getMinutes()) + '' ;
    return date;
};

$(function () {
    var loading = false; //状态标记
    var hasNextPage = false;
    var pageNo = 1;
    queryList(pageNo);

    //下拉刷新
    $("#weipai").pullToRefresh().on("pull-to-refresh", function () {
        $("#weipai-list-content .left-list").html("");
        $("#weipai-list-content .right-list").html("");
        pageNo = 1;
        queryList(pageNo,function(){
            $("#weipai").pullToRefreshDone(); // 重置下拉刷新
        });
    });

    //上拉加载更多
    $('#weipai').infinite().on("infinite", function () {
        if (hasNextPage){
            pageNo++;
            queryList(pageNo,function(){
                
            },true);
        }        
    });

    function queryList(pageNo,callback,isMore){
        if (loading) return;
        loading = true;
        $.post('/antique/api/userProductCollection/query_list', {
            start: (pageNo - 1) * 20,
            limit: 20,
            token: getCookie('token')
        }, function (result) {
            if (!isMore){
                var leftList = [];
                var rightList = [];
                if(result.data.list){
                    for(var i = 0;i<result.data.list.length;i++){
                        if(i%2 == 0){
                            leftList.push(result.data.list[i]);
                        } else {                        
                            rightList.push(result.data.list[i]);
                        }
                    }
                }
                /* global template */
                document.getElementById('left-list-content').innerHTML = template('left-list', {list:leftList});
                document.getElementById('right-list-content').innerHTML = template('right-list', {list:rightList});            
            } else {
                $("#weipai-list-content").append(template('weipai-list', result.data));
            }

            $('.weipai-list .lot .info .price .time time[sign="unmake"]').each(function () {
                $(this).removeAttr('sign');
                /* global Countdown */
                Countdown($(this).attr('time'), $(this));
            });
            hasNextPage = result.data.hasNextPage;
            if (!result.data.hasNextPage) {
                $('#loadmore').hide();
                $('#nomore').show();
            } else {
                $('#loadmore').show();
                $('#nomore').hide();
            }
            if (callback) callback();
            loading = false;

            //添加删除事件
            $('.weipai-list .lot .info .price .del').click(function(){
                var productId = $(this).attr('productId');
                $.post('/antique/api/userProductCollection/set_collection', {
                    productId:productId,
                    token: getCookie('token'),
                    operType:2
                }, function (result) {
                    $.toast("删除成功!",function(){
                        window.location.reload();
                    },1000);
                });
                return false;
            });
        });
    }

    $('.weipai-list .op .clear').click(function(){
        $.confirm("您确定清空所有关注的藏品吗?", "确认清空?", function() {
            $.post('/antique/api/userProductCollection/clear_collection_log', {
                token: getCookie('token')
            }, function (result) {
                $.toast("清空关注藏品成功!",function(){
                    window.location.reload();
                },1000);
            });
        }, function() {
            //取消操作
        });
    });
});